<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    li {
        list-style: none;
        border-bottom: 1px solid lightcoral;
        display: block;
        box-sizing: border-box;
        padding-top: 10px;
    }

    oCont {
        width: 400px;
    }

    .content {
        width: 500px;
        height: 200px;
        overflow-y: scroll;
        border: 1px solid lightseagreen;
    }

    a {
        text-decoration: none;
        color: brown;
        display: block;
        float: right;
        cursor: pointer;
        user-select: none;
    }
</style>

<body>

    昵称：<input type="text" class="userName"><br />
    <textarea name="" id="" cols="30" rows="10"></textarea>
    <button>发布</button>

    <p>留言：</p>
    <ul class="content">

    </ul>

    <script>

        //获取元素
        var oUser = document.querySelector('.userName');
        var oText = document.querySelector('textarea');
        var btn = document.querySelector('button');
        var oUl = document.querySelector('.content');

        btn.onclick = function () {

            //判断输入是否为空
            if (oText.value == '' || oUser.value == '') {
                alert('请输入昵称和内容')
                return false
            }

            //创建li
            var oLi = document.createElement('li');

            //显示li内容
            oLi.innerHTML = oUser.value + '<br>' + oText.value + '<a>删除</a>';
            //获取内容
            oUl.appendChild(oLi);
            //清除输入框的内容
            oText.value = '';

            //删除
            var oA = document.querySelectorAll('a');

            for (var i = 0; i < oA.length; i++) {
                oA[i].onclick = function () {
                    oLi.remove(this.parentNode);
                }
            }


        }
    </script>

</body>

</html>